package editor;

import game.Livello;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

public class PannelloEditor extends JPanel implements MouseListener, Runnable {
	
	private Editor editor;
	
	/**
	 * Crea un nuovo oggetto di tipo PannelloEditor.
	 * @param editor oggetto di tipo Editor.
	 */
	public PannelloEditor(Editor editor){
		super();
		this.editor = editor;
		this.setPreferredSize(new Dimension(editor.getWidth() * Livello.TILE_SIZE,12 * Livello.TILE_SIZE));
		this.setMaximumSize(new Dimension(1600,600));
		this.setBackground(Color.WHITE);
		this.setFocusable(true);
		this.addMouseListener(this);
		new Thread(this).start();
		
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.setColor(editor.getSfondoEditor());
		g.fillRect(0, 0, (editor.getWidth()*Livello.TILE_SIZE), (editor.getHeight())*Livello.TILE_SIZE);
		g.drawImage(editor.getImmagineSfondo(),0,0,null);
		g.setColor(Color.GRAY);
		int righe =  (this.getPreferredSize().height)/Livello.TILE_SIZE;
		int colonne =  (this.getPreferredSize().width)/Livello.TILE_SIZE;
		for(int i=0;i <= righe ;i++){
			g.drawLine(0, (i* Livello.TILE_SIZE), (colonne * Livello.TILE_SIZE), (i*Livello.TILE_SIZE));	
		}
		for (int j=0; j<=colonne; j++){
			g.drawLine((j * Livello.TILE_SIZE), 0, (j * Livello.TILE_SIZE), (righe*Livello.TILE_SIZE));	
		}
		for (SpriteImage imm : editor.getImages()){
			g.drawImage(imm.getImmagine(),imm.getX(),imm.getY(),null);
		}
		
	}

	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}

	public void mousePressed(MouseEvent e) {
		int moduloX = e.getX() % Livello.TILE_SIZE;
		int moduloY = e.getY() % Livello.TILE_SIZE;
		int coordX = e.getX() - moduloX;
		int coordY = e.getY() - moduloY;
		if ((e.getY() < (editor.getHeight())*Livello.TILE_SIZE) && (e.getX() < (editor.getWidth())*Livello.TILE_SIZE )){
			if (e.getButton() == MouseEvent.BUTTON1){
				editor.setModificato(true);
				if(editor.getDrawItem()!=null){
					eliminaElemento(coordX,coordY);
					editor.getImages().add(new SpriteImage(editor.getDrawItem(),coordX,coordY,editor.getDrawItemType()));
					editor.aggiornaMatrice(editor.getSimbolo(), coordY/Livello.TILE_SIZE, coordX/Livello.TILE_SIZE);
				}
			}
			else if (e.getButton() == MouseEvent.BUTTON3){
				editor.setModificato(true);
				eliminaElemento(coordX,coordY);
			}
		}
	}
	
	/**
	 * Elimina l'elemento con coordinate (x,y) dal vettore delle
	 * immaigini da disegnare sul pannello.
	 * @param x coordinata x dell'elemento.
	 * @param y coordinate y dell'elemento.
	 */
	private void eliminaElemento (int x, int y){
		SpriteImage temp = new SpriteImage(null,0,0,' ');
		for (SpriteImage imm : editor.getImages()){
			if ((imm.getX() == x) && (imm.getY() == y)){
				temp = imm;
			}
		}
		editor.getImages().remove(temp);
		editor.aggiornaMatrice(' ', y/Livello.TILE_SIZE, x/Livello.TILE_SIZE);
	}
	
	public void run() {
		while(true){
			this.repaint();
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
